package com.white.commonlib.manager;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by ytf on 2017/3/20 020.
 * Description: 线程池
 */

public class ThreadManager
{
    private static ThreadManager instance;

    static final int EXECUTOR_THREADS = Runtime.getRuntime().availableProcessors();

    ExecutorService es;

    private ThreadManager()
    {
        es = Executors.newCachedThreadPool();
    }

    public static ThreadManager getInstance()
    {
        if(instance == null)
        {
            synchronized(ThreadManager.class)
            {
                if(instance == null)
                {
                    instance = new ThreadManager();
                }
            }
        }
        return instance;
    }

    public ExecutorService get()
    {
        return es;
    }

    /***
     * 关闭线程池，app关闭时应该调用该方法
     */
//    public void shutdown()
//    {
//        if(threadPool != null && (!threadPool.isShutdown()))
//        {
//            try
//            {
//                threadPool.awaitTermination(100, TimeUnit.MILLISECONDS);
//            } catch (InterruptedException e)
//            {
//                e.printStackTrace();
//                threadPool.shutdown();
//            }
//        }
//    }
}
